/**
 * @BelongsProject : LeetCode
 * @Author : WXY
 * @CreateTime : 2022-12-08  19:03
 * @Description :剑指 Offer 26. 树的子结构
 * @Version : 1.0
 */

public class Num026_isSubStructure {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (A == null) return false;
        if (B == null) return false;
        return curSubStructure(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
    }

    private boolean curSubStructure(TreeNode A, TreeNode B) {
        if (B == null)return true;
        if (A == null)return false;
        return A.val == B.val && curSubStructure(A.left, B.left) && curSubStructure(A.right, B.right);
    }

}
